import java.util.*;

public class Text {
    public static void main(String[] args) {
        String s = "leetcode";
        List<String> wordDict = new ArrayList<>();
        wordDict.add("leet");
        wordDict.add("code");
        System.out.println(wordBreak(s, wordDict));
    }
    public static boolean wordBreak(String s, List<String> wordDict) {
        Set<String> hashset = new HashSet<>(wordDict);
        int n = s.length();
        boolean[] dp = new boolean[n+1];
        dp[0] = true;
        s = " " + s;
        for(int i = 1;i <= n;i++) {
            for(int j = i;j >= 1;j--) {
                if(dp[j-1] && hashset.contains(s.substring(j,i+1))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
}
